public class LengthOfLongestSubstring {
    // leetcode 无重复字符的最长子串
    // https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/?envType=study-plan-v2&envId=top-100-liked
    public int lengthOfLongestSubstring(String s) {
        int left = 0, right = 0;
        int len = s.length();
        int maxLength = 0;
        int[] hash = new int[128];
        while (right < len)  {
            while (right < len && ++hash[s.charAt(right)] == 1) right++;
            maxLength = Math.max(maxLength, right - left);
            while (left < right && --hash[s.charAt(left)] != 1) left++;
            right++;
            left++;
        }
        return maxLength;
    }
}
